package com.wasu.sdk_ott.http.network.error;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wasu.UILApplication;

/* <p>------------------------------------------------------
 * <p>Copyright (C) 2021 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p>
 *
 * <p>
 *
 * @author yingjh
 * @date on  2021/10/29 16:00.
 */
public class Report {
    private final static String TAG = "Report";

    public static void report(String errorCode, String errorReason) {
        Error error = ErrorUtils.errorBean(errorCode, errorReason);
        sendError(UILApplication.getInstance().getApplication(), error);
    }

    public static void report(String errorCode, String errorReason, String serviceName, String url, String urlBody, String result) {
        Error error = ErrorUtils.errorBean(errorCode, errorReason);
        error.setServiceName(serviceName);
        error.setUrl(url);
        error.setUrlBody(urlBody);
        error.setResult(result);
        sendError(UILApplication.getInstance().getApplication(), error);
    }

    /**
     * 解析url和post body,错误上报
     *
     * @param posturl 网络请求地址
     * @param urlbody post body数据
     */
    public static void reportEmptybyUrl(String posturl, String urlbody) {
        if (TextUtils.isEmpty(posturl)) {
            return;
        }
        /*请求视频广告接口不上报，系统端不返回数据，请求太过频繁*/
        if (posturl.contains("iads_ol/ad")) {
            return;
        }
        ErrorUtils.Edata edata = ErrorUtils.get_edata(posturl, urlbody);
        Error error = ErrorUtils.errorBean(edata.errorCode, edata.errorReason);
        String serviceName = ErrorUtils.getServieName(posturl);
        error.setServiceName(serviceName);
        error.setUrl(posturl);
        error.setUrlBody(urlbody);
        error.setResult("返回无数据");
        sendError(UILApplication.getInstance().getApplication(), error);
    }

    public static void print(String posturl, String urlbody) {
        ErrorUtils.Edata edata = ErrorUtils.get_edata(posturl, urlbody);
        Error error = ErrorUtils.errorBean(edata.errorCode, edata.errorReason);
        String serviceName = ErrorUtils.getServieName(posturl);
        error.setServiceName(serviceName);
        error.setUrl(posturl);
        error.setUrlBody(urlbody);
        Gson gson = new Gson();
        String beanstr = gson.toJson(error);
        Log.d(TAG, " print ---message =" + beanstr);
    }

    public static void sendError(Context context, Error error) {
        //Gson gson = new Gson();
        //String beanstr = gson.toJson(error);
        String beanstr = JSON.toJSONString(error);
        Log.d(TAG, "sdk plugin sendError =" + beanstr);
        Intent intent = new Intent();
        intent.setClassName("com.wasu.launcher", "com.wasu.error.receiver.ErrorReceiver");
        //intent.setAction("com.wasu.action_ERROR_REPORT_RECEIVER");
        intent.putExtra("key_error_report", beanstr);
        context.sendBroadcast(intent);
    }
}
